package com.lg.config;

import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.LinkedHashMap;

import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;

@Configuration
public class ShiroConfig {

	/**
	 * Filter Chain定义说明：
	 * 
	 * 1、一个URL可以配置多个Filter，使用逗号分隔 2、当设置多个过滤器时，全部验证通过，才视为通过 3、部分过滤器可指定参数，如perms，roles
	 * Shiro内置的FilterChain
	 * 
	 * Filter Name Class anon org.apache.shiro.web.filter.authc.AnonymousFilter
	 * authc org.apache.shiro.web.filter.authc.FormAuthenticationFilter authcBasic
	 * org.apache.shiro.web.filter.authc.BasicHttpAuthenticationFilter perms
	 * org.apache.shiro.web.filter.authz.PermissionsAuthorizationFilter port
	 * org.apache.shiro.web.filter.authz.PortFilter rest
	 * org.apache.shiro.web.filter.authz.HttpMethodPermissionFilter roles
	 * org.apache.shiro.web.filter.authz.RolesAuthorizationFilter ssl
	 * org.apache.shiro.web.filter.authz.SslFilter user
	 * org.apache.shiro.web.filter.authc.UserFilter\
	 *  anon:所有url都都可以匿名访问 
	 *  authc: 需要认证才能进行访
	 *问 //user:配置记住我或认证通过可以访问
	 * 
	 * @param securityManager
	 * @return
	 */
	@Bean
	public ShiroFilterFactoryBean shirFilter(SecurityManager securityManager) {
		ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
		shiroFilterFactoryBean.setSecurityManager(securityManager);
		// 拦截器
		LinkedHashMap<String, String> filterChainDefinitionMap = new LinkedHashMap<>();
		filterChainDefinitionMap.put("/sb/*.html", "anon");
		filterChainDefinitionMap.put("/registe", "anon");
		// 配置退出 过滤器,其中的具体的退出代码Shiro已经替我们实现了
		filterChainDefinitionMap.put("/logout", "logout");
		// <!-- 过滤链定义，从上向下顺序执行，一般将/**放在最为下边 -->:这是一个坑呢，一不小心代码就不好使了;
		// <!-- authc:所有url都必须认证通过才可以访问; anon:所有url都都可以匿名访问-->
		filterChainDefinitionMap.put("/**", "authc");
		// 如果不设置默认会自动寻找Web工程根目录下的"/login.jsp"页面
		shiroFilterFactoryBean.setLoginUrl("/login");
		// 登录成功后要跳转的链接
		shiroFilterFactoryBean.setSuccessUrl("/index");
		// 未授权界面;
		shiroFilterFactoryBean.setUnauthorizedUrl("/403");
		shiroFilterFactoryBean.setFilterChainDefinitionMap(filterChainDefinitionMap);
		return shiroFilterFactoryBean;
	}

	@Bean
	public SecurityManager securityManager(MyRealm MyRealm) {
		DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
		securityManager.setRealm(MyRealm);
		return securityManager;
	}

	
}
